package com.zhanglu.fun.services.datacenter.token;

import com.zhanglu.fun.services.datacenter.entity.AuthToken;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description
 * @Auther zhanglu
 * @Date 2017/9/20 下午1:57
 */
@RestController
@RequestMapping("/auth")
@Slf4j
public class TokenRouter {

    @Autowired
    private TokenService tokenService;

    @ApiOperation(value = "根据token查询", notes = "根据token查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token令牌", required = true, dataType = "String", paramType = "query")
    })
    @ApiResponses({ @ApiResponse(code = 200, message = "请求成功", response = AuthToken.class) })
    @GetMapping("/token")
    public AuthToken getAuthTokenByToken(@RequestParam("token") String token) {
        log.info("getAuthTokenByToken receive param is:[token is {}]", token);
        return tokenService.getAuthTokenByToken(token);
    }

    @ApiOperation(value = "添加token", notes = "添加token")
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功", response = Boolean.class)})
    @PutMapping("/token")
    public Boolean addAuthTokenByToken(@RequestBody AuthToken authToken) {
        log.info("addAuthTokenByToken receive param is [object to string:{}]", authToken.toString());
        return tokenService.addAuthTokenByToken(authToken);
    }

    @ApiOperation(value = "修改token", notes = "修改token")
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功", response = Boolean.class)})
    @PostMapping("/token")
    public Boolean updateAuthTokenById(@RequestBody AuthToken authToken) {
        log.info("updateAuthTokenByToken receive param is [object to string:{}]", authToken.toString());
        return tokenService.updateAuthTokenByToken(authToken);
    }

    @ApiOperation(value = "根据token删除", notes = "根据token删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token令牌", required = true, dataType = "String", paramType = "query")
    })
    @ApiResponses({ @ApiResponse(code = 200, message = "请求成功", response = Boolean.class) })
    @DeleteMapping("/token")
    public Boolean deleteAuthTokenByToken(@RequestParam("token") String token) {
        log.info("getAuthTokenByToken receive param is:[token is {}]", token);
        return tokenService.deleteAuthTokenByToken(token);
    }

    @ApiOperation(value = "根据user表id主键查询token", notes = "根据user表id主键查询token")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "user表id主键", required = true, dataType = "Long", paramType = "path")
    })
    @ApiResponses({ @ApiResponse(code = 200, message = "请求成功", response = AuthToken.class) })
    @GetMapping("/token/{userId}")
    public AuthToken getAuthTokenByUserId(@PathVariable("userId") Long userId) {
        log.info("getAuthTokenByUserId receive param is:[login id is {}]", userId);
        return tokenService.getAuthTokenByUserId(userId);
    }
}
